package com.xx.bysj.utils;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

@Component
public class UploadUtils {
    /**
     * 上传保存的地址
     */
    public static  String BASE_PATH = System.getProperty("user.dir");
    //访问图片的地址

    private static final String SERVER_PATH = "/static/upload/image/";

    public static String upload(MultipartFile file, HttpServletRequest request){


        request.getSession().setAttribute("file",file);
        MultipartFile file1 = (MultipartFile) request.getSession().getAttribute("file");

        String BASE_PATH_TO = BASE_PATH + "\\src\\main\\resources\\static\\upload\\image\\";
        BASE_PATH = BASE_PATH+"\\src\\main\\resources\\static\\show\\image\\";
        //获取上传文件的名称
        String fileName = file.getOriginalFilename();
//       用于判断是否只由数字跟字母组成
        String pattern = "[\\w]+[.]+[\\w]+";
        if (!fileName.matches(pattern)) {
            return null;
        }
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String newFileName = uuid +"-"+fileName;
        // 创建一个文件实例对象
        File image = new File(BASE_PATH,newFileName);
        File imageFile = new File(BASE_PATH_TO, newFileName);
        System.out.println("image = " + image);
        System.out.println("imageFile = " + imageFile);
        //对文件进行上传操作 file 只能使用一次用完后就关闭了
        try {
            file.transferTo(image);
        } catch (IOException e) {
            return null;
        }
        //对图片进行复制
        try {
            copyImage(image,imageFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //返回请求路径
        return SERVER_PATH + newFileName;
    }

    /**
     * 图片复制
     * @param image
     * @param imageFile
     * @throws IOException
     */
    public static void copyImage(File image, File imageFile) throws IOException {
        FileInputStream in=new FileInputStream(image);
        FileOutputStream out= new FileOutputStream(imageFile);
        BufferedInputStream bufferedIn=new BufferedInputStream(in);
        BufferedOutputStream bufferedOut=new BufferedOutputStream(out);
        byte[] by=new byte[1];
        while (bufferedIn.read(by)!=-1) {
            bufferedOut.write(by);
        }
        //将缓冲区中的数据全部写出
        bufferedOut.flush();
        bufferedIn.close();
        bufferedOut.close();
    }

}
